# -*- coding: utf-8 -*-
# !/usr/bin/env python
from utils.mysql_utils import query_to_list


def query_env_host_app(env_id=''):
    sql_parameter = []

    if env_id:
        sql_fragment = "WHERE b.env_id = %s"
        sql_parameter.append(env_id)
    else:
        sql_fragment = ''

    sql = """
    SELECT a.*,b.host_ip,c.package_name,d.soft_nick,e.sys_code_name AS package_type_name, f.sys_code_name AS package_repository_name
    FROM apps_configuration a
    LEFT JOIN hosts_management b ON a.host_id = b.host_id AND b.`status` = 1
    LEFT JOIN package_management c ON c.id = a.package_id AND c.`status` = 1
    LEFT JOIN soft_management d ON d.soft_id = a.soft_id AND d.`status` = 1
    LEFT JOIN sys_code e ON c.package_type_code = e.sys_code AND e.sys_code_type = "package_type"
    LEFT JOIN sys_code f ON c.package_repository_code = f.sys_code AND f.sys_code_type = "package_repository_type"
    %s 
    AND a.`status` = 1
    """ % sql_fragment
    data = query_to_list(sql, sql_parameter, False)

    return data
